package com.spnice.sgsms.controller;

import com.spnice.sgsms.enums.ResultEnum;
import com.spnice.sgsms.service.AnalysisService;
import com.spnice.sgsms.util.WrappedResult;
import com.spnice.sgsms.vo.SettingVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class AnalysisController {
    @Autowired
    private AnalysisService analysisService;

    @PostMapping("/analysisPDF")
    public WrappedResult analysisPDF(@RequestParam("file") MultipartFile file) {
        List<SettingVo> settingVos = analysisService.analysisPDF(file);
        if (CollectionUtils.isEmpty(settingVos)) {
            return WrappedResult.failedWrappedResult(ResultEnum.FILE_ANALYSIS_ERROR);
        }
        return WrappedResult.successWrapedResult(settingVos);
    }

    @PostMapping("/pdfToStandardExcel")
    public void pdfToStandardExcel(@RequestParam("file") MultipartFile file,
                                   HttpServletResponse response) {
        analysisService.pdfToStandardExcel(file, response);
    }
}
